/**
 * 
 */
package org.william.common_boot.database;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/** 
 * Description: 
 * 解析配置项
		定制主库、从库 -以及以及指向到SeqSessionFactory
 * @author xuhongwei
 * @version 1.0
 * <pre>
 * Modification History: 
 * Date         Author      Version     Description 
------------------------------------------------------------------
 * 2018年4月11日    xuhongwei     1.0        1.0 Version 
 * </pre>
 */

@Configuration
public class DataSourceConfiguration {
	
	private Logger log=LoggerFactory.getLogger(DataSourceConfiguration.class);
	
	 @Value("${spring.datasource.type}")
     private Class<? extends DataSource> dataSourceType;
	 
	 
    @Bean(name = "masterDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource masterDataSource() {
        log.info("-------------------- masterDataSource init ---------------------");
        return DataSourceBuilder.create().type(dataSourceType).build();
    }
    
    
    /**
     *  
     * Description: 
     * @param
     * @return Object
     * @throws
     * @Author xuhongwei
     * Create Date: 2018年4月11日
     */
    @Bean(name = "slaveDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    public DataSource slaveDataSource() {
        log.info("-------------------- slaveDataSource init ---------------------");
        return DataSourceBuilder.create().type(dataSourceType).build();
    }
	 
    @Bean(name = "dataSource")  
	public DataSource dataSource(){
		
    	DynamicDataSource dynamicDataSource = new DynamicDataSource();
    	
    	// 配置多数据源  
        Map<Object, Object> targetDataSources = new HashMap<Object, Object>();  	
    
        targetDataSources.put("master", this.masterDataSource());
        
        targetDataSources.put("slave", this.slaveDataSource());
        
        dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
        
        dynamicDataSource.setTargetDataSources(targetDataSources);
        
        return dynamicDataSource;
        
	}
    
    
//    @Bean(name = "dataSource")  
//	public DataSource dataSource(){
//		
//        DriverManagerDataSource dataSource = new DriverManagerDataSource();
//
//        
//        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//        dataSource.setUrl("jdbc:mysql//localhost:3306/db_master");
//        dataSource.setUsername("root");
//        dataSource.setPassword("william0071");
//        
//        return dataSource;
//        
//	}
    
    
    
    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(this.dataSource());
        return factoryBean.getObject();
    }
	 
}
